import { get } from "@/utils/request";
import OSS from "ali-oss";
export async function file2base64(file) {
    return new Promise((r, j) => {
        const reader = new FileReader();
        reader.readAsDataURL(file);
        reader.onload = (evt) => {
            r(evt.target.result);
        };
        // reader.onerror = e => j(e);
    });
}
/**
 * oss上传文件
 * @param {string} type 文件类型 middle 私有文件 cdn 公共读文件 disk 归档文件
 * @param {string} fileName 文件名
 * @param {Object} file 文件
 * @param {Function} progress 上传进度回调
 * @param {Function} clientCall client 回调
 * client.cancel(); 取消上传
 * @returns {String} 文件路径
 */
export const ossUpload = async (
    type,
    fileName,
    file,
    progress = (p, checkpoint) => {},
    clientCall = (client) => {},
    contentType = "image/jpeg"
) => {
    const res = await get("/user/cu/sys/base/js-oss-up-auth", {
       type,
        fileName: fileName || file.name,
    });
    const {data} = res;
    const client = new OSS(data);
    if (clientCall && typeof clientCall === "function") {
        clientCall(client);
    }
    const result = await client.multipartUpload(data.path, file, {
        progress,
        headers:{
            "Content-Type": contentType
        }
    });
    return result.name;
};

// export const downFile = async(
//     type,
//     fileName,
//     resourceUrl
// ) => {
//     const data = await get("/user/cu/sys/base/js-oss-up-auth", {
//         type,
//         fileName: fileName || file.name,
//     });
//     const client = new OSS(data);
//     try {
//         let result = await client.signatureUrl(resourceUrl);
//         console.log(result);
//     } catch (error) {
//         console.log(error);
//     }
// }


